package com.zzy.model;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class UtilModel {
    public static void main(String[] args) {
        //modelRepeat();
        //modelRepeat2();
        modelRepeat3HashCode();
    }

    //实体 去重复
    public static void modelRepeat(){
        GroupModel g1 = new GroupModel("group01","分组01",1.0);
        GroupModel g2 = new GroupModel("group01","分组01",1.0);
        GroupModel g3 = new GroupModel("group03","分组03",1.0);
        List<GroupModel> list = new ArrayList<>();
        list.add(g1);
        list.add(g2);
        list.add(g3);
        List<GroupModel> resList = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(()-> new TreeSet<>(Comparator.comparing(GroupModel::getGroupId)))
                ,ArrayList::new
        ));
        System.out.println(resList);
    }


    //实体 去重复
    public static void modelRepeat2(){
        GroupModel g1 = new GroupModel("group01","分组01",1.0);
        GroupModel g2 = new GroupModel("group01","分组01",1.0);
        GroupModel g3 = new GroupModel("group03","分组03",1.0);
        List<GroupModel> list = new ArrayList<>();
        list.add(g1);
        list.add(g2);
        list.add(g3);

        Set<GroupModel> set = new TreeSet<>( (o1,o2)->o1.getGroupId().compareTo(o2.getGroupId()) );
        set.addAll(list);
        List<GroupModel> resList = new ArrayList<>(set);
        System.out.println(resList);
    }



    //实体 HashCode 去重复
    public static void modelRepeat3HashCode(){
        GroupModel g1 = new GroupModel("group01","分组01",1.0);
        GroupModel g2 = new GroupModel("group01","分组01",1.0);
        GroupModel g3 = new GroupModel("group03","分组03",1.0);
        Set<GroupModel> set = new HashSet<>();
        set.add(g1);
        set.add(g2);
        set.add(g3);
        List<GroupModel> resList = new ArrayList<>(set);
        System.out.println(resList);
    }
}
